/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.migrate.cbc;

import com.wtkj.vo.dzzf.base.kfwd.FundAccount;
import com.wttech.dzzf.process.dao.CbcBankDataCheckDao;
import com.wttech.dzzf.process.service.base.BaseInfoService;
import com.wttech.dzzf.process.service.migrate.FundAccountProcessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

import static com.wttech.dzzf.process.constant.UserConstants.DEFAULT_PROCESS_NUM;

/**
 * 合并处理建行增量数据
 *
 * @author MaShibo
 * @date 2017/12/25.
 */
@Service
public class MergateAddDataService {

	private static final Logger LOGGER = LoggerFactory.getLogger(MergateAddDataService.class);

	@Resource
	private CbcBankDataCheckDao cbcBankDataCheckDao;

	@Resource
	private ThreadPoolTaskExecutor executor;

	@Resource
	private BaseInfoService baseInfoService;

	@Resource
	private MergateFundAccountService mergateFundAccountService;

	@Resource(name = "fundAccountHisDataProcess")
	private FundAccountProcessService fundAccountProcessService;


	private void process() {
		int total = cbcBankDataCheckDao.queryFundAccountNum();
		LOGGER.info("共{}条资金账户信息待处理", total);
		int corePoolSize = executor.getCorePoolSize();
		int maxPerSize = corePoolSize * DEFAULT_PROCESS_NUM;
		int processNum;
		int cout;
		if (total < maxPerSize) {
			cout = corePoolSize;
			processNum = (int) Math.ceil((double) total / corePoolSize);
		} else {
			processNum = DEFAULT_PROCESS_NUM;
			cout = (int) Math.ceil((double) total / processNum);
		}
		LOGGER.info("每次约处理{}条记录，共需要处理{}次", processNum, cout);
		List<Future<Void>> futures = new ArrayList<>(cout);
		for (int i = 0; i < cout; i++) {
			int num = i + 1;
			int startRow = (num - 1) * processNum + 1;
			int endRow = num * processNum > total ? total : num * processNum;
			futures.add(executor.submit(() -> {
				processOnce(startRow, endRow);
				return null;
			}));
		}
		baseInfoService.getFutureResult(futures);
		executor.shutdown();
	}

	/**
	 * 单次处理数据
	 *
	 * @param startRow 起始行
	 * @param endRow   结束行
	 */
	private void processOnce(int startRow, int endRow) {
		LOGGER.info("开始处理第【{}】行至第【{}】行数据", startRow, endRow);
		List<FundAccount> fundAccounts = cbcBankDataCheckDao.queryPageFundAccountInfos(startRow, endRow);
		fundAccounts.forEach(this::processFundAccount);
		LOGGER.info("第【{}】行至第【{}】行数据处理完成", startRow, endRow);
	}

	/**
	 * 处理资金账户信息
	 *
	 * @param fundAccount 资金账户
	 */
	private void processFundAccount(FundAccount fundAccount) {
		try {
			mergateFundAccountService.process(fundAccount);
//			fundAccountProcessService.process(fundAccount);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
	}

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		MergateAddDataService service = context.getBean(MergateAddDataService.class);
		service.process();
	}
}
